package day5;

import java.util.ArrayList;
import java.util.List;

public class demo3 {
    public static void main(String[] args) {
        System.out.println(LastRemaining_Solution(5, 3));
    }

     static int LastRemaining_Solution (int n, int m) {
        List<Integer>list=new ArrayList<>();
        for (int i = 0; i <n ; i++) {
            list.add(i);
        }
        int cur=-1;
        int count=-1;
        while(list.size()>1){
            count++;
            cur++;
            if(cur>=list.size()){
                cur=-1;
            }
            if(count==m-1){
                if (cur==-1){list.remove(0);}
                else {
                    list.remove(cur);
                }

                count=-1;
            }
        }
        return list.get(0);
    }

}
